#include "BlockQueue.hpp"
#include "Thread.hpp"

#include <string>
#include <unistd.h>
#include <vector>

void Consumer(BlockQueue<int> *&bq)
{
    while(true)
    {
        cout << "Consumer: " << bq << endl;
        sleep(1);
    }
} 
void Productor(BlockQueue<int> *&bq)
{
    while(true)
    {
        cout << "Productor: " << bq << endl;
        sleep(1);
    }
}

void StartComm(vector<yss::Thread<BlockQueue<int>*>>* threads, int num, 
BlockQueue* bq, yss::func_t<BlockQueue<int>*> func)//命名空间作用域！！！
{
    for(int i=0; i<num; i++)
    {
        string name = "thread-" + to_string(i+1);
        threads->emplace_back(func, bq, name);
        threads->back().Start();
    }
}
void StartConsumer(vector<yss::Thread<BlockQueue<int>*>>* threads, int num, BlockQueue<int>* bq)
{
    StartComm(threads, num, bq, Consumer);
}
void StartProductor(vector<yss::Thread<BlockQueue<int>*>>* threads, int num, BlockQueue<int>* bq)
{
    StartComm(threads, num, bq, Productor);
}

void WaitAllThread(vector<yss::Thread<BlockQueue<int>*>>& threads)
{
    for(auto& thread:threads)
    {
        thread.Join();
    }
}

int main()
{
    BlockQueue<int>* bq = new BlockQueue(5);
    vector<yss::Thread<BlockQueue<int>*>> threads;
    StartConsumer(&threads, 1, bq);
    StartProductor(&threads, 1, bq);
    WaitAllThread(threads);
    return 0;
}